//
//  TLMyBonusHeaderView.m
//  WXReader
//
//  Created by huaya on 2025/9/5.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLMyBonusHeaderView.h"
#import "TLRewardsViewController.h"

@interface TLMyBonusHeaderView ()

@property (weak, nonatomic) IBOutlet UILabel *bonusLab;

@property (weak, nonatomic) IBOutlet UILabel *expireLab;

@property (weak, nonatomic) IBOutlet UIButton *getFreeBonusBtn;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

@implementation TLMyBonusHeaderView

+ (instancetype)createView {
    return [[[NSBundle mainBundle] loadNibNamed:@"TLMyBonusHeaderView" owner:nil options:nil] lastObject];
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
}

- (void)drawRect:(CGRect)rect{
    
    self.getFreeBonusBtn.layer.masksToBounds = YES;
    self.getFreeBonusBtn.layer.cornerRadius = 20;
    
    [self.bottomView addRoundingCornersWithRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight andCornerRadii:20];
    
}
- (void)setHeaderModel:(TLMyBonusModel *)headerModel{
    if (headerModel) {
        _headerModel = headerModel;
        
        _bonusLab.text = [NSString stringWithFormat:@"%@",_headerModel.totalCoins];
        
        _expireLab.text = [self formatTimestampToUTC7:_headerModel.tomorrowExpireTime.longLongValue andBonus:_headerModel.tomorrowExpireCoins];
    }
}
- (NSString *)formatTimestampToUTC7:(long)timestamp andBonus:(NSNumber *)bonus {
    // 1. 将秒级时间戳转换为 NSDate
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
    
    // 2. 获取手机系统的当前时区
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    
    // 3. 配置日期格式化器
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"yyyy/MM/dd HH:mm"];
    
    // 4. 强制使用英文月份格式
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [formatter setLocale:locale];
    
    // 5. 格式化日期并拼接字符串
    NSString *dateString = [formatter stringFromDate:date];
    return [NSString stringWithFormat:@"%@ Bonus expire on %@",bonus, dateString];
}
- (IBAction)getFreeBonusAction:(id)sender {
    TLRewardsViewController *vc = [TLRewardsViewController new];
    [[TL_ViewHelper getCurrentViewController].navigationController pushViewController:vc animated:YES];
    
}


@end
